<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="就诊人id" prop="patientId">
      <el-input v-model="dataForm.patientId" placeholder="就诊人id"></el-input>
    </el-form-item>
    <el-form-item label="医生id" prop="doctorId">
      <el-input v-model="dataForm.doctorId" placeholder="医生id"></el-input>
    </el-form-item>
    <el-form-item label="预约日期" prop="reserveDate">
      <el-input v-model="dataForm.reserveDate" placeholder="预约日期"></el-input>
    </el-form-item>
    <el-form-item label="预约时间（0：上午 1：下午）" prop="reserveTime">
      <el-input v-model="dataForm.reserveTime" placeholder="预约时间（0：上午 1：下午）"></el-input>
    </el-form-item>
    <el-form-item label="取号地点" prop="fetchAddress">
      <el-input v-model="dataForm.fetchAddress" placeholder="取号地点"></el-input>
    </el-form-item>
    <el-form-item label="医事服务费" prop="amount">
      <el-input v-model="dataForm.amount" placeholder="医事服务费"></el-input>
    </el-form-item>
    <el-form-item label="退号时间" prop="quitTime">
      <el-input v-model="dataForm.quitTime" placeholder="退号时间"></el-input>
    </el-form-item>
    <el-form-item label="订单状态(0:未支付,1:取消预约,2:预约成功)" prop="orderStatus">
      <el-input v-model="dataForm.orderStatus" placeholder="订单状态(0:未支付,1:取消预约,2:预约成功)"></el-input>
    </el-form-item>
    <el-form-item label="支付时间" prop="payTime">
      <el-input v-model="dataForm.payTime" placeholder="支付时间"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="订单号" prop="orderNum">
      <el-input v-model="dataForm.orderNum" placeholder="订单号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          patientId: '',
          doctorId: '',
          reserveDate: '',
          reserveTime: '',
          fetchAddress: '',
          amount: '',
          quitTime: '',
          orderStatus: '',
          payTime: '',
          createTime: '',
          orderNum: ''
        },
        dataRule: {
          patientId: [
            { required: true, message: '就诊人id不能为空', trigger: 'blur' }
          ],
          doctorId: [
            { required: true, message: '医生id不能为空', trigger: 'blur' }
          ],
          reserveDate: [
            { required: true, message: '预约日期不能为空', trigger: 'blur' }
          ],
          reserveTime: [
            { required: true, message: '预约时间（0：上午 1：下午）不能为空', trigger: 'blur' }
          ],
          fetchAddress: [
            { required: true, message: '取号地点不能为空', trigger: 'blur' }
          ],
          amount: [
            { required: true, message: '医事服务费不能为空', trigger: 'blur' }
          ],
          quitTime: [
            { required: true, message: '退号时间不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态(0:未支付,1:取消预约,2:预约成功)不能为空', trigger: 'blur' }
          ],
          payTime: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hos/appointment/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.patientId = data.appointment.patientId
                this.dataForm.doctorId = data.appointment.doctorId
                this.dataForm.reserveDate = data.appointment.reserveDate
                this.dataForm.reserveTime = data.appointment.reserveTime
                this.dataForm.fetchAddress = data.appointment.fetchAddress
                this.dataForm.amount = data.appointment.amount
                this.dataForm.quitTime = data.appointment.quitTime
                this.dataForm.orderStatus = data.appointment.orderStatus
                this.dataForm.payTime = data.appointment.payTime
                this.dataForm.createTime = data.appointment.createTime
                this.dataForm.orderNum = data.appointment.orderNum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hos/appointment/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'patientId': this.dataForm.patientId,
                'doctorId': this.dataForm.doctorId,
                'reserveDate': this.dataForm.reserveDate,
                'reserveTime': this.dataForm.reserveTime,
                'fetchAddress': this.dataForm.fetchAddress,
                'amount': this.dataForm.amount,
                'quitTime': this.dataForm.quitTime,
                'orderStatus': this.dataForm.orderStatus,
                'payTime': this.dataForm.payTime,
                'createTime': this.dataForm.createTime,
                'orderNum': this.dataForm.orderNum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
